﻿using AndroidXml;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using ICSharpCode.SharpZipLib.Zip;
using System.Xml.Linq;

namespace AndroidTools
{
    public class ApkInfo
    {
        private string path;
        private FileStream fileStream;
        public ManifestInfo manifestInfo = new ManifestInfo();


        public ApkInfo(string path)
        {
            this.path = path;
            InitManifestInfo();
        }


        private void OpenFileStream()
        {
            fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
        }

        private void CloseFileStream()
        {
            try
            {
                fileStream.Close();
            }
            catch
            {

            }
        }

        private void InitManifestInfo() {
            OpenFileStream();
            byte[] manifestData = null;

            ZipFile zipfile = new ZipFile(fileStream);
            foreach (ZipEntry item in zipfile)
            {
                if (item.Name.ToLower() == "androidmanifest.xml")
                {
                    using (Stream strm = zipfile.GetInputStream(item))
                    {
                        using (BinaryReader s = new BinaryReader(strm))
                        {
                            manifestData = s.ReadBytes((int)item.Size);
                        }
                    }
                    break;
                }
            }


            #region 读取文件内容
            using (var stream = new MemoryStream(manifestData))
            {
                AndroidXmlReader reader = new AndroidXmlReader(stream);
                ReadNode(reader);
                reader.Close();
            }
            #endregion
        }

        /// <summary>
        /// 读取节点信息
        /// </summary>
        /// <param name="reader"></param>
        public void ReadNode(AndroidXmlReader reader)
        {
            XDocument doc = XDocument.Load(reader);

            // 获取manifest节点
            XElement manifest = doc.Element("manifest");
            manifestInfo.PackageName = manifest.Attribute("package").Value;

            // 获取application节点
            XElement application = manifest.Element("application");

            // 获取入口activity
            foreach (XElement activity in application.Elements("activity"))
            {
                if (FindMainActivity(activity))
                {
                    break;
                }
            }

            if (manifestInfo.MainActivity == null)
            {
                // 获取入口activity
                foreach (XElement activity in application.Elements("activity-alias"))
                {

                    if (FindMainActivity(activity))
                    {
                        break;
                    }
                }
            }

        }


        public bool FindMainActivity(XElement activity)
        {
            // 命名空间
            XNamespace androidNamespace = "http://schemas.android.com/apk/res/android";

            XElement intentFilter = activity.Element("intent-filter");
            if (intentFilter != null)
            {
                bool isMAIN = false;
                foreach (XElement action in intentFilter.Elements("action"))
                {
                    XAttribute actionAttr = action.Attribute(androidNamespace + "name");
                    if (actionAttr != null && actionAttr.Value == "android.intent.action.MAIN")
                    {
                        isMAIN = true;
                        break;
                    }
                }
                bool isLAUNCHER = false;
                if (isMAIN)
                {
                    foreach (XElement category in intentFilter.Elements("category"))
                    {
                        XAttribute categoryAttr = category.Attribute(androidNamespace + "name");
                        if (categoryAttr != null && categoryAttr.Value == "android.intent.category.LAUNCHER")
                        {
                            isLAUNCHER = true;
                            break;
                        }
                    }
                }

                if (isMAIN && isLAUNCHER)
                {
                    manifestInfo.MainActivity = activity.Attribute(androidNamespace + "name").Value;
                    return true;
                }
            }

            return false;
        }
    }


    /// <summary>
    /// AndroidManifest.xml
    /// </summary>
    public class ManifestInfo
    {
        /// <summary>
        /// 包名称
        /// </summary>
        public string PackageName { get; set; }
        /// <summary>
        /// 入口activity
        /// </summary>
        public string MainActivity { get; set; }

    }
}